import Vue from "vue"
import Router from "vue-router"

import routes from "./common/config/router.js"

Vue.use(Router)
//解决 Vue 重复点击相同路由，出现 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation 问题
const VueRouterPush = Router.prototype.push
Router.prototype.push = function push(to) {
	return VueRouterPush.call(this, to).catch(err => err)
}

// export default new Router({ routes });
let router = new Router({routes})

// 全局前置守卫
router.beforeEach((to, from, next) => {
	const corpid = to.query.corpid
	// 获取token
	let corpidSession = sessionStorage.getItem('corpid');
	if(corpidSession){ // 已登录
		// 防止重复登录
		if(to.path === '/login'){
			Vue.prototype.$message.error('请不要重复登录');
			return next({name:from.name ? from.name : 'index'})
		}
		// 其他验证...
		next();
	} else {
		// 跳过登录页验证
		if(to.path === '/login'){
			return next();
		}
		// 未登录
		// Vue.prototype.$message.error('请先登录');
		// next({ path:'/login' })
		 next(`/login?corpid=${corpid}`)
	}
})

export default router
